package pers.huangyuhui.sms.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.huangyuhui.sms.model.AdminInfo;
import pers.huangyuhui.sms.model.StudentInfo;
import pers.huangyuhui.sms.model.TeacherInfo;
import pers.huangyuhui.sms.service.AdminService;
import pers.huangyuhui.sms.service.StudentService;
import pers.huangyuhui.sms.service.TeacherService;
import pers.huangyuhui.sms.util.StringUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {

    // 获取 Service 层的对象
    @Resource(name = "adminService")
    private AdminService adminService;
    @Resource(name = "teacherService")
    private TeacherService teacherService;
    @Resource(name = "studentService")
    private StudentService studentService;

    /**
     * @param name 用户名
     * @param password 密码
     * @param session session作用域
     * @param response response请求方式
     * @throws IOException IOException
     */
    @PostMapping("loginController/login.do")
    private void login(@RequestParam(value = "userName" , required = false , defaultValue = "") String name,
                       @RequestParam(value = "userPassword" , required = false , defaultValue = "") String password,
                       @RequestParam(value = "userType" , required = false , defaultValue = "1") Integer userType,
//                       @RequestParam(value = "verificationCode" , required = false , defaultValue = "") String verificationCode,
                       HttpSession session,
                       HttpServletResponse response) throws IOException {

//        String rightCode = (String) session.getAttribute("rightCode");
//
//        System.out.println( "userName:" + name +
//                            ",userPassword:" + password +
//                            ",rightCode:" + rightCode +
//                            ",sessionId:" + session.getId() +
//                            ",verificationCode:" + verificationCode);
//        System.out.println(adminService.toString());
//        System.out.println(teacherService.toString());
//        System.out.println(studentService.toString());
//
//        // 验证码确认
//        if (StringUtil.isEmpty(verificationCode) || !verificationCode.equals(rightCode)) {
//            response.getWriter().write("vCodeError");
//            return;
//        }

        // 验证用户信息
        switch (userType) {
            case 1:
                // 获取用户
                AdminInfo adminInfo = adminService.getUserInfo(name , password);
                if (adminInfo == null) {
                    // 登陆失败
                    response.getWriter().write("loginError");
                    return;
                }
                // 登录成功: 将用户信息存储到Sesssion
                session.setAttribute("userInfo", adminInfo);
                session.setAttribute("userType", userType);
                // 进入系统主页
                response.getWriter().write("loginSuccess");
                break;
            case 2:
                // 逻辑同 case 1
                StudentInfo studentInfo = studentService.getUserInfo(name , password);
                if (studentInfo == null) {
                    response.getWriter().write("loginError");
                    return;
                }
                session.setAttribute("userInfo", studentInfo);
                session.setAttribute("userType", userType);
                response.getWriter().write("loginSuccess");
                break;
            case 3:
                // 逻辑同 case 1
                TeacherInfo teacherInfo = teacherService.getUserInfo(name , password);
                if (teacherInfo == null) {
                    response.getWriter().write("loginError");
                    return;
                }
                session.setAttribute("userInfo", teacherInfo);
                session.setAttribute("userType", userType);
                response.getWriter().write("loginSuccess");
                break;
        }
    }

    @GetMapping("loginOut.do")
    private String logout(HttpSession session){
        session.removeAttribute("userInfo");
        session.removeAttribute("userType");

        return "redirect:index.jsp";
    }
}
